<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class News extends Model
{
	//新增新闻
	public function addnews($param)
	{
        $title = News::get(['title'=>$param['title']]);
        if(isset($title)){
			$this->error = '已存在相同名称的新闻';
        	return false;
        }
        $this->startTrans();
		try{
			$this->data($param)->allowField(true)->save();
			$this->commit();
			return true;
		}catch(\Exception $e){
			$this->rollback();
			$this->error = '添加失败';
			return false;
		}
	}

	//更新新闻
	public function updatenews($param)
	{
		$news = News::get($param['id']);
		$this->startTrans();
		try{
			$news['title'] = $param['title'];
			$news['content'] = $param['content'];
			$news['img'] = $param['img'];
			$news->allowField(true)->save();
			$this->commit();
			return true;
		}catch(\Exception $e){
			$this->rollback();
			$this->error = $e;
			return false;
		}
	}

	//删除新闻
	public function deletenews($param)
	{
		$news = News::get($param);
		$this->startTrans();
		try{
			$news->delete();
			$this->commit();
			return true;
		}catch(\Exception $e){
			$this->rollback();
			$this->error = '删除失败';
			return false;
		}
	}
	//发布新闻
	public function fabunews($param)
	{
		$news = News::get($param);
		$this->startTrans();
		try{
			$news['status'] = 1;
			$news->allowField(true)->save();
			$this->commit();
			return true;
		}catch(\Exception $e){
			$this->rollback();
			$this->error = '删除失败';
			return false;
		}
	}
	//取消发布
	public function cancelnews($param)
	{
		$news = News::get($param);
		$this->startTrans();
		try{
			$news['status'] = 0;
			$news->allowField(true)->save();
			$this->commit();
			return true;
		}catch(\Exception $e){
			$this->rollback();
			$this->error = '删除失败';
			return false;
		}
	}
}